Conditions | 1 |
Paths | 128 |
Total Lines | 144 |
Code Lines | 92 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | define(['snabbdom', 'helper', 'moment'], function (V, helper, moment) { |
||
2 | 'use strict'; |
||
3 | V = V.default; |
||
4 | |||
5 | var self = {}; |
||
6 | |||
7 | function showBar(v, width, warning) { |
||
8 | return V.h('span', |
||
9 | { props: { className: 'bar' + (warning ? ' warning' : '') } }, |
||
10 | [ |
||
11 | V.h('span', |
||
12 | { |
||
13 | style: { width: (width * 100) + '%' } |
||
14 | }), |
||
15 | V.h('label', v) |
||
16 | ] |
||
17 | ); |
||
18 | } |
||
19 | |||
20 | self.showStatus = function showStatus(d) { |
||
21 | return V.h('td', |
||
22 | { props: { className: d.is_online ? 'online' : 'offline' } }, |
||
23 | _.t((d.is_online ? 'node.lastOnline' : 'node.lastOffline'), { |
||
24 | time: d.lastseen.fromNow(), |
||
25 | date: d.lastseen.format('DD.MM.YYYY, H:mm:ss') |
||
26 | })); |
||
27 | }; |
||
28 | |||
29 | self.showGeoURI = function showGeoURI(d) { |
||
30 | if (!helper.hasLocation(d)) { |
||
31 | return undefined; |
||
32 | } |
||
33 | |||
34 | return V.h('td', |
||
35 | V.h('a', |
||
36 | { props: { href: 'geo:' + d.location.latitude + ',' + d.location.longitude } }, |
||
37 | Number(d.location.latitude.toFixed(6)) + ', ' + Number(d.location.longitude.toFixed(6)) |
||
38 | ) |
||
39 | ); |
||
40 | }; |
||
41 | |||
42 | self.showGateway = function showGateway(d) { |
||
43 | return d.is_gateway ? _.t('yes') : undefined; |
||
44 | }; |
||
45 | |||
46 | self.showFirmware = function showFirmware(d) { |
||
47 | return [ |
||
48 | helper.dictGet(d, ['firmware', 'release']), |
||
49 | helper.dictGet(d, ['firmware', 'base']) |
||
50 | ].filter(function (n) { |
||
51 | return n !== null; |
||
52 | }).join(' / ') || undefined; |
||
53 | }; |
||
54 | |||
55 | self.showUptime = function showUptime(d) { |
||
56 | return moment.utc(d.uptime).local().fromNow(true); |
||
57 | }; |
||
58 | |||
59 | self.showFirstSeen = function showFirstSeen(d) { |
||
60 | return d.firstseen.fromNow(true); |
||
61 | }; |
||
62 | |||
63 | self.showLoad = function showLoad(d) { |
||
64 | return showBar(d.loadavg.toFixed(2), d.loadavg / (d.nproc || 1), d.loadavg >= d.nproc); |
||
65 | }; |
||
66 | |||
67 | self.showRAM = function showRAM(d) { |
||
68 | return showBar(Math.round(d.memory_usage * 100) + ' %', d.memory_usage, d.memory_usage >= 0.8); |
||
69 | }; |
||
70 | |||
71 | self.showDomain = function showDomain(d) { |
||
72 | var rt = d.domain; |
||
73 | if (config.domainNames) { |
||
74 | config.domainNames.some(function (t) { |
||
75 | if (rt === t.domain) { |
||
|
|||
76 | rt = t.name; |
||
77 | return true; |
||
78 | } |
||
79 | }); |
||
80 | } |
||
81 | return rt; |
||
82 | }; |
||
83 | |||
84 | self.showClients = function showClients(d) { |
||
85 | if (!d.is_online) { |
||
86 | return undefined; |
||
87 | } |
||
88 | |||
89 | var clients = [ |
||
90 | V.h('span', [ |
||
91 | d.clients > 0 ? d.clients : _.t('none'), |
||
92 | V.h('br'), |
||
93 | V.h('i', { props: { className: 'ion-people', title: _.t('node.clients') } }) |
||
94 | ]), |
||
95 | V.h('span', |
||
96 | { props: { className: 'legend-24ghz' } }, |
||
97 | [ |
||
98 | d.clients_wifi24, |
||
99 | V.h('br'), |
||
100 | V.h('span', { props: { className: 'symbol', title: '2,4 GHz' } }) |
||
101 | ]), |
||
102 | V.h('span', |
||
103 | { props: { className: 'legend-5ghz' } }, |
||
104 | [ |
||
105 | d.clients_wifi5, |
||
106 | V.h('br'), |
||
107 | V.h('span', { props: { className: 'symbol', title: '5 GHz' } }) |
||
108 | ]), |
||
109 | V.h('span', |
||
110 | { props: { className: 'legend-others' } }, |
||
111 | [ |
||
112 | d.clients_other, |
||
113 | V.h('br'), |
||
114 | V.h('span', { props: { className: 'symbol', title: _.t('others') } }) |
||
115 | ]) |
||
116 | ]; |
||
117 | |||
118 | return V.h('td', { props: { className: 'clients' } }, clients); |
||
119 | }; |
||
120 | |||
121 | self.showIPs = function showIPs(d) { |
||
122 | var string = []; |
||
123 | var ips = d.addresses; |
||
124 | ips.sort(); |
||
125 | ips.forEach(function (ip, i) { |
||
126 | if (i > 0) { |
||
127 | string.push(V.h('br')); |
||
128 | } |
||
129 | |||
130 | if (ip.indexOf('fe80:') !== 0) { |
||
131 | string.push(V.h('a', { props: { href: 'http://[' + ip + ']/', target: '_blank' } }, ip)); |
||
132 | } else { |
||
133 | string.push(ip); |
||
134 | } |
||
135 | }); |
||
136 | return V.h('td', string); |
||
137 | }; |
||
138 | |||
139 | self.showAutoupdate = function showAutoupdate(d) { |
||
140 | return d.autoupdater.enabled ? _.t('node.activated', { branch: d.autoupdater.branch }) : _.t('node.deactivated'); |
||
141 | }; |
||
142 | |||
143 | return self; |
||
144 | }); |
||
145 |
This check looks for functions where a
return
statement is found in some execution paths, but not in all.Consider this little piece of code
The function
isBig
will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly returnundefined
.This behaviour may not be what you had intended. In any case, you can add a
return undefined
to the other execution path to make the return value explicit.